<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="net.zdsoft.keel.util.DateUtils" %>
<%@ page import="net.zdsoft.keel.util.StringUtils" %>
<%@ page import="net.zdsoft.keel.util.Validators" %>
<%@ page import="net.zdsoft.keel.util.ArrayUtils" %>
<%@ page import="net.zdsoft.wpchat.support.util.PageUtils" %>
<%@ page import="net.zdsoft.wpchat.support.entity.AuthCatalog" %>
<%@ page import="net.zdsoft.wpchat.support.entity.AuthOperation" %>
<%@ page import="net.zdsoft.wpchat.support.entity.AuthGroup" %>
<%@ page import="net.zdsoft.wpchat.support.entity.GroupPermission" %>
<%@ page import="net.zdsoft.wpchat.support.enums.PermissionTypeEnum" %>
<%@ page import="net.zdsoft.wpchat.support.entity.UserConfig"%>
<%@ page import="net.zdsoft.wpchat.support.action.auth.AuthPermissionAction" %>
<%
AuthPermissionAction action = (AuthPermissionAction) PageUtils.getAction();
String groupId=StringUtils.htmlFilterToEmpty(action.getGroupId());
String catalogId=StringUtils.htmlFilterToEmpty(action.getCatalogId());
List<AuthGroup> groupList = action.getGroupList();

List<AuthCatalog> allCatalogs = action.getAuthConfig().getCatalogs();
List<AuthCatalog> catalogList = action.getCatalogList();

Map<String, GroupPermission> opId2GroupPermMap = action.getOpId2GroupPermMap();

String display = !Validators.isEmpty(catalogId) ? "" : "display:none";
String currentGroupName = null;
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>后台</title>
<style type="text/css" media="all">@import url("../../css/style.css");</style>
<script language="JavaScript">
<!--
function selectAll(groupId){
  var operations=document.getElementsByName("operationId"); 
  var selectAll=document.getElementById(groupId + "selectAll");

  for (var i=0,length=operations.length;i<length;i++){
    if (operations[i].title==groupId){
      operations[i].checked=selectAll.checked;
    } 
  }   
}

function selectAllCatalog(){
  var operations=document.permList.elements;
  var selectAll=document.getElementById("selectAllCheckBox");
  for (var i=0,length=operations.length;i<length;i++){
    if (operations[i].type=="checkbox"){
      operations[i].checked=selectAll.checked;
    } 
  }
}

function ToggleMenuOnOff (menuName) {
  menu = document.getElementById(menuName);
  expandImg=document.getElementById(menuName + "Img");
  if (menu.style.display == 'none') {
    menu.style.display = '';
    expandImg.src="../../images/expand-closed.gif";
    expandImg.title="单击关闭详细信息";
  } else {
    menu.style.display = 'none';
    expandImg.src="../../images/expand-open.gif";
    expandImg.title="单击显示详细信息";
  }
}
//-->
</script>
</head>
<body>
<div id="tableMain">
  <div id="message"><%=PageUtils.errors(action)%></div>
  <div id="message"><%=PageUtils.successMessage("操作成功")%></div>
  <div id="condition">
    <form name="listForm" method="get" action="listPermission.htm">   
    用户组：
    <select id="groupId" name="groupId" onchange="submit();" ><%
for (int i = 0, length = groupList.size(); i < length; i++) {
    AuthGroup group = (AuthGroup)groupList.get(i);
    String selected = null;
    if (group.getId().equals(action.getGroupId())) {
        currentGroupName = group.getName();
        selected = " selected=\"selected\"";
    }
    else {
        selected = "";
    } %>
      <option value="<%=group.getId()%>"<%=selected%>><%=group.getName()%></option> <%
} %>
    </select>
    目录：
    <select id="catalogId" name="catalogId" onchange="submit()">
      <option value="">所有</option><%
for (int i = 0, length1 = allCatalogs.size(); i < length1; i++) {
    AuthCatalog catalog = (AuthCatalog) allCatalogs.get(i);
    List<AuthOperation> actions = action.getAuthConfig().getOperations(catalog.getId());
    String selected = catalog.getId().equals(action.getCatalogId()) ? " selected=\"selected\"" : "";
    if (actions.isEmpty()) {
        continue;
    } %>
      <option value="<%=catalog.getId()%>"<%=selected%>><%=catalog.getName()%></option> <%
} %>
    </select>        
    </form>
  </div>
  <form name="permList" method="post" action="authPermissionAdd.htm">
  <input type="hidden" name="groupId" value="<%=groupId%>" />
  <input type="hidden" name="catalogId" value="<%=catalogId%>" /><%
if (Validators.isEmpty(catalogId)) { %>  
  <div id="message"><input id="selectAllCheckBox" name="selectAllCheckBox" type="checkbox" onclick="selectAllCatalog()" />所有模块</div><%
}
for (int i = 0, length1 = catalogList.size(); i < length1; i++) {
    AuthCatalog catalog = (AuthCatalog)catalogList.get(i); 
    List<AuthOperation> actions = action.getAuthConfig().getOperations(catalog.getId());
    if (actions.isEmpty()) {
        continue;
    } %>
  <div id="message">
    <a href="javascript:ToggleMenuOnOff('<%=catalog.getId()%>')"><img id="<%=catalog.getId()%>Img" src="../../images/expand-open.gif" title="单击显示详细信息" border="0" /></a><input id="<%=catalog.getId()%>selectAll" name="<%=catalog.getId()%>selectAll" type="checkbox" onclick="selectAll('<%=catalog.getId()%>')" /><%=catalog.getName()%>
  </div>      
  <table class="dataTable" id="<%=catalog.getId()%>" style="<%=display%>" border="0" align="center" cellpadding="5" cellspacing="1"><%
    for (int ii = 0,length2 = actions.size(); ii < length2; ii++) {
        AuthOperation operation = (AuthOperation) actions.get(ii);
        String checked = "";
        GroupPermission perm = new GroupPermission();
        perm.setPermission(Integer.parseInt(operation.getScope()[0]));
        if (opId2GroupPermMap.containsKey(operation.getId())) {
            checked = " checked=\"checked\"";
            perm = opId2GroupPermMap.get(operation.getId());
        } %>
    <tr>
      <td width="20%" class="titleTd">
        <input id="operationId" name="operationId" group="<%=catalog.getId()%>" title="<%=catalog.getId()%>" type="checkbox" value="<%=operation.getId()%>" <%=checked%> />
      </td>
      <td width="80%">
        <%=operation.getModule().getName() + "—" + operation.getLabel() +"(" + operation.getName() + "[" + ArrayUtils.toString(operation.getAction())+  "])"%>
        <input type="hidden" id="<%=operation.getId()%>_Permision" name="<%=operation.getId()%>_Permision" value="1" />
      </td>
    </tr><%
    } %>
  </table><%
}
if (!UserConfig.ROLES_ADMIN.equals(currentGroupName)) { %>
  <div id="buttonRight"><input type="submit" name="Submit" class="colorButton" value=" 保存 "/></div><%
} %>
  </form>
</div>
</body>
</html>
